local abs = math.abs

local function time(name, f)
	local before = os.clock()
	f()
	local after = os.clock()
	print(name .. ": " .. (after - before))
end

local create_closure = obitstream_factory([[
	local n = 0
]], [[
	local next_n = n + 1
	if next_n == 65 then
		next_n = 1
	end
	table[next_n] = buf
	n = next_n
]], "")

local create_in = ibitstream_factory([[
	local n = 0
	local max_n = 512
	local max_bits = 17
]], [[
	local next_n = n + 1

	if next_n >= max_n then
		if next_n > max_n then
			next_n = 1
		else
			bits_in_buf = max_bits
		end
	end
	buf = table[next_n]
	n = next_n
]], [[
	function table.reset()
		table.in_reset()
		n = 0
	end
]])

time("c", function()
	local putint = math.mul
	for i=1,100000000 do
		putint(i, 14)
	end
end)



time("lua", function()
	local l = create_closure()
	local putint = l.putint
	for i=1,100000000 do
		putint(i, 14)
	end
end)

--[[
local bs3 = create_closure2()

time("closure2", function()
	local l = bs3
	for i=1,100000000 do
		l:putint(i, 14)
	end
end)
]]